package com.xzj.admin.boot.controller;

import com.xzj.admin.boot.domain.vo.SysUserVO;
import com.xzj.common.id.gen.DefaultIdHelper;
import com.xzj.common.secure.annotation.XzjEncrypt;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.boot.domain.entity.SysUser;
import com.xzj.admin.boot.service.SysUserService;

/**
 * @program: xzj-admin
 * @ClassName: SysUserController
 * @description: 用户表;(sys_user)表控制层
 * @author: XiaZunJun
 * @create: 2024-7-30
 */
@Tag(name = "用户表对象功能接口")
@RestController
@RequestMapping("/sysUser")
public class SysUserController{
    @Resource
    private SysUserService sysUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{userId}")
    public SysUser queryById(@PathVariable(name = "userId")String userId){
        return sysUserService.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<SysUserVO> pageQuery(@RequestBody PageReq<SysUserVO> pageReq){
        return sysUserService.pageQuery(pageReq);
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    @XzjEncrypt
    public SysUserVO addOrUpdate(@RequestBody SysUserVO sysUser){
        if(ObjectUtil.isNotEmpty(sysUser.getUserId())){
            sysUserService.update(sysUser);
        }else{
            sysUser.setUserId(DefaultIdHelper.nextIdStr());
            sysUserService.insert(sysUser);
        }
        return sysUser;
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{userId}")
    public boolean deleteById(@PathVariable(name = "userId") long userId){
        return sysUserService.deleteById(userId);
    }
}